package com.krtech.wecard.module.ai;

/*
引入依赖包
<!-- https://mvnrepository.com/artifact/com.aliyun/videoenhan20200320 -->
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>videoenhan20200320</artifactId>
  <version>${aliyun.videoenhan.version}</version>
</dependency>
*/

import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoFaceResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

public class MergeVideoFace {
    public static com.aliyun.videoenhan20200320.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
        return new com.aliyun.videoenhan20200320.Client(config);
    }

    public static void main(String[] args) throws Exception {
//        LocalFace();

        UrlFace();
    }

    public static void UrlFace() throws Exception {
        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        String accessKeyId = "LTAI5tAK8JHwnntxE5ERvhbT";
        String accessKeySecret = "Y0A67pO2DloUcZDaZGpJrFtriCsDsP";
        com.aliyun.videoenhan20200320.Client client = MergeVideoFace.createClient(accessKeyId, accessKeySecret);
        com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest mergeVideoFaceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest()
                .setVideoURL("https://pcxoss.oss-cn-shanghai.aliyuncs.com/1744893700795310.mp4?Expires=1744897957&OSSAccessKeyId=TMP.3KqZ26LLFDGieEBrk8mwh93nTgXodq5AsL1iFr6V6S6HQCsMcsdZtnFs71MWYhC2TLQmRcLPbtE5LbQHXacXKJZMHyABxY&Signature=yHolCRPNG5o7YTXkjtS9UixuWDc%3D")
                .setReferenceURL("https://pcxoss.oss-cn-shanghai.aliyuncs.com/WechatIMG47387.jpeg?Expires=1744897966&OSSAccessKeyId=TMP.3KqZ26LLFDGieEBrk8mwh93nTgXodq5AsL1iFr6V6S6HQCsMcsdZtnFs71MWYhC2TLQmRcLPbtE5LbQHXacXKJZMHyABxY&Signature=3bjIVDXY4a9L6bLYa9gPwI2yk6k%3D");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
//            MergeVideoFaceResponse mergeVideoFaceResponse = client.mergeVideoFace(mergeVideoFaceRequest);
            MergeVideoFaceResponse mergeVideoFaceResponse = client.mergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoFaceResponse)));
            // 获取单个字段
            System.out.println(mergeVideoFaceResponse.getBody());
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }

    public static void LocalFace() throws Exception{
        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        com.aliyun.videoenhan20200320.Client client = MergeVideoFace.createClient(accessKeyId, accessKeySecret);
        // 场景一，使用本地文件
        //mp4
        InputStream inputStream1 = new FileInputStream(new File("/Users/pengchengxuan/Downloads/1744893700795310.mp4"));
         //png
        InputStream inputStream2 = new FileInputStream(new File("/Users/pengchengxuan/Downloads/WechatIMG47387.jpeg"));
        // 场景二，使用任意可访问的url
        com.aliyun.videoenhan20200320.models.MergeVideoFaceAdvanceRequest mergeVideoFaceAdvanceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoFaceAdvanceRequest()
                .setVideoURLObject(inputStream1)
                .setReferenceURLObject(inputStream2);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            MergeVideoFaceResponse mergeVideoFaceAdvanceResponse = client.mergeVideoFaceAdvance(mergeVideoFaceAdvanceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoFaceAdvanceResponse)));
            // 获取单个字段
            System.out.println(mergeVideoFaceAdvanceResponse.getBody());
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}